package com.xinke.module.system.controller.admin.user;

import cn.hutool.core.collection.CollUtil;
import com.xinke.module.system.convert.user.UserConvert;
import com.xinke.module.system.manager.user.AdminUserManager;
import com.xinke.module.system.model.entity.user.DeptDO;
import com.xinke.module.system.service.user.DeptService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.annotation.security.PermitAll;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.xinke.component.common.pojo.PageParam;
import com.xinke.component.common.pojo.PageResult;
import com.xinke.component.common.pojo.Result;
import com.xinke.component.common.util.object.BeanUtils;
import static com.xinke.component.common.pojo.Result.success;

import com.xinke.component.excel.excel.core.util.ExcelUtils;

import com.xinke.component.web.apilog.core.annotation.ApiAccessLog;

import static com.xinke.component.common.util.collection.CollectionUtils.convertList;
import static com.xinke.component.web.apilog.core.enums.OperateTypeEnum.*;

import com.xinke.module.system.model.vo.user.*;
import com.xinke.module.system.model.entity.user.AdminUserDO;
import com.xinke.module.system.service.user.AdminUserService;

@Tag(name = "管理后台 - 用户信息")
@RestController
@RequestMapping("/system/user")
@Validated
public class UserController {

    @Resource
    private AdminUserService adminUserService;

    @Resource
    private AdminUserManager adminUserManager;
    @Resource
    private DeptService deptService;
    @PostMapping("/create")
    @Operation(summary = "创建用户信息")
    @PermitAll
//    @PreAuthorize("@ss.hasPermission('system:user:create')")
    public Result<Long> createAdminUser(@Valid @RequestBody AdminUserSaveReqVO createReqVO) {
        return success(adminUserManager.createAdminUser(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户信息")
    @PreAuthorize("@ss.hasPermission('system:user:update')")
    public Result<Boolean> updateAdminUser(@Valid @RequestBody AdminUserSaveReqVO updateReqVO) {
        adminUserManager.updateUser(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户信息")
    @Parameter(name = "userId", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:user:delete')")
    public Result<Boolean> deleteAdminUser(@RequestParam("userId") Long userId) {
        adminUserManager.deleteUser(userId);
        return success(true);
    }

    @GetMapping("/info")
    @Operation(summary = "获得用户信息")
    @Parameter(name = "userId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:user:query')")
    public Result<AdminUserRespVO> getAdminUser(@RequestParam("userId") Long userId) {
        AdminUserDO adminUser = adminUserService.getAdminUser(userId);
        return success(BeanUtils.toBean(adminUser, AdminUserRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户信息分页")
    @PreAuthorize("@ss.hasPermission('system:user:query')")
    public Result<PageResult<AdminUserRespVO>> getAdminUserPage(@Valid AdminUserPageReqVO pageReqVO) {
        PageResult<AdminUserDO> pageResult = adminUserService.getAdminUserPage(pageReqVO);
        if (CollUtil.isEmpty(pageResult.getList())) {
            return success(new PageResult<>(pageResult.getTotal()));
        }
        // 拼接数据
        Map<Long, DeptDO> deptMap = deptService.getDeptMap(
                convertList(pageResult.getList(), AdminUserDO::getDeptId));
        return success(new PageResult<>(UserConvert.INSTANCE.convertList(pageResult.getList(), deptMap),
                pageResult.getTotal()));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户信息 Excel")
    @PreAuthorize("@ss.hasPermission('system:user:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAdminUserExcel(@Valid AdminUserPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AdminUserDO> list = adminUserService.getAdminUserPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户信息.xls", "数据", AdminUserRespVO.class,
                        BeanUtils.toBean(list, AdminUserRespVO.class));
    }

}